<template>
  <div class="tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div ref="table_container" style="margin-top:20px" class="table_container">
          <el-table
            ref="table"
            :data="tableData"
            stripe
            border
            :height="tableHeight"
            :row-class-name="rowClassName"
            :header-cell-class-name="headerCellClassName"
            @row-click="rowClick"
            @selection-change="
              (val) => {
                selectionChange(val, this);
              }
            "
          >
            <el-table-column type="selection" width="70" align="center" :selectable="tableSelectable" />
            <Column
              v-for="(column, index) in defaultTableColumns"
              :key="index"
              :prop="column.prop"
              :is-show="column.isShow"
              :label="column.label"
              :sortable="column.sortable"
              :min-width="column.minWidth"
              :width="column.width"
            />
            <el-table-column
              prop="purchase"
              label="采购数量"
              fixed="right"
              min-width="200"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.purchaseNum" :disabled="scope.row.isStatus" clearable placeholder="请输入" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    layerid: {},
    id: {},
    index: {},
    row: {}
  },
  data() {
    return {
      loading: false,
      pageId: 'chooseApplicationNumber', // 页面唯一标识
      typeId: '',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [
        // 表格常规字段（初始值）
        { prop: 'saleOrderCode', label: '销售订单号' },
        { prop: 'orderByName', label: '销售人员' },
        { prop: 'orderTypeCodeName', label: '订单类型' },
        { prop: 'saleCustomerName', label: '客户名称' },
        { prop: 'orderGoodsTime', label: '订货日期' },
        { prop: 'contractCode', label: '合同号' }
      ]

    }
  },
  mounted() {
    this.tableData = this.$deepClone(this.row.saleOrderDetails)

    if (this.row.details && this.row.details.length > 0) {
      this.selectionsAll = this.$deepClone(this.row.details)

      this.tableData.forEach(item => {
        const cacheItem = this.selectionsAll.filter(item2 => item.id == item2.id)
        if (cacheItem && cacheItem.length > 0) {
          item.purchaseNum = cacheItem[0].purchaseNum
        }
      })
    }
    this.$nextTick(() => {
      this.setSelectRow(this)// 设置行选中
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    tableSelectable(row, index) {
      // if (row.isStatus) {
      //   return false
      // } else {
      return true
      // }
    },
    rowClick(row, column) {
      // if ((!column) || (column && (column.property === 'purchase')) || row.isStatus) {
      // } else {
      //   this.$refs.table.toggleRowSelection(row)
      // }
    },
    headerCellClassName(data) {
      if (['purchase'].indexOf(data.column.property) != -1) {
        return 'column_bt'
      }
    },
    rowClassName({ row, rowIndex }) {
      if (this.selectionsAll.length > 0 && ((this.selectionsAll.some(item => item.id == row.id)))) {
        return 'rowSelected'
      }
    },
    cancel() {
      this.$layer.close(this.layerid)
    },
    onSubmit() {
      if (this.selectionsAll && this.selectionsAll.length <= 0) {
        this.$message.warning('请勾选采购明细')
        return
      }
      if (this.selectionsAll && this.selectionsAll.filter(item => !item.purchaseNum).length > 0) {
        this.$message.warning('采购数量不能为空')
        return
      } else if (this.selectionsAll && this.selectionsAll.filter(item => !this.SETTINGS.reg_no_zero.test(item.purchaseNum)).length > 0) {
        this.$message.warning('采购数量未正确填写')
        return
      }
      this.$parent.chooseApplicationNumberCallBack(this.selectionsAll, this.row, this.index)
      this.$layer.close(this.layerid)
    }
  }
}
</script>

<style lang="less" scoped>
.main_main {
  height: calc(100% - 50px);
  margin: 0;
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

}
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.table_container{
  margin-top: 20px !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 0px !important;
}
.main_main{
  margin-right:0 !important;
  padding-top:0 !important;
}
.button_icon{
  background:#387bfc;
  width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
/deep/ .el-input__inner {
  text-align: left !important;
}
.formItem_header {
  padding-left: 20px;
}
/**
 * 解决el-input设置类型为number时，中文输入法光标上移问题
 **/
/deep/.el-input__inner{
  line-height: 1px !important;
}

/**
 * 解决el-input设置类型为number时，去掉输入框后面上下箭头
 **/
/deep/ input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.accessory {
  color: #387bfc
}
</style>
